package com.lbd.gulimall.ware.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;


import com.lbd.common.exception.RRException;
import com.lbd.gulimall.ware.vo.FareVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lbd.gulimall.ware.entity.WareInfoEntity;
import com.lbd.gulimall.ware.service.WareInfoService;
import com.lbd.common.utils.PageUtils;
import com.lbd.common.utils.R;


/**
 * 仓库信息
 *
 * @author linbd
 * @email 2236000738@qq.com
 * @date 2021-06-04 17:25:56
 */
@RestController
@RequestMapping("ware/wareinfo")
public class WareInfoController {
    @Autowired
    private WareInfoService wareInfoService;

    //这个请求不走网关，需要单独进行跨域配置，且前端通过withCredentials携带kooie，需要指定白名单，不可配“*”
    @CrossOrigin(origins = "http://order.gulimall.com",allowCredentials = "true")
    @RequestMapping("/fare")
    public R getFare(@RequestParam("addrId") Long addrId) {
        FareVO fareVO;
        try {
            fareVO = wareInfoService.getFare(addrId);
        } catch (ExecutionException | InterruptedException | RRException e) {
            return R.error(e.getMessage());
        }
        return R.ok().put("data", fareVO);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:wareinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareInfoService.queryByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:wareinfo:info")
    public R info(@PathVariable("id") Long id) {
        WareInfoEntity wareInfo = wareInfoService.getById(id);

        return R.ok().put("wareInfo", wareInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:wareinfo:save")
    public R save(@RequestBody WareInfoEntity wareInfo) {
        wareInfoService.save(wareInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:wareinfo:update")
    public R update(@RequestBody WareInfoEntity wareInfo) {
        wareInfoService.updateById(wareInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:wareinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        wareInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
